package com.mokee.android.ShipPass;

import java.util.ArrayList;

import com.mokee.android.Fragment.MeFragment;
import com.mokee.android.Fragment.MyFragmentPagerAdapter;
import com.mokee.android.Fragment.OtherFragment;
import com.mokee.android.Fragment.PayFragment;
import com.mokee.android.Pay.PayNewActivity;
import com.mokee.android.database.DataBaseHelper;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends FragmentActivity implements OnClickListener {
	private static final String tag = "MainActivity";
	
	private static MainActivity instance = null;
	private ViewPager mViewPager;
	private ArrayList<Fragment> fragmentList = null;
	private LinearLayout linearLayout_More, linearLayout_Pay, linearLayout_Me;
	private ImageView iv_More, iv_Pay, iv_Me;
	private TextView tv_More, tv_Pay, tv_Me;
	//Top Layout
	private LinearLayout layout_Add;
	private ImageView iv_TopImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		initView();
        initEvent();
        instance = this;
	}

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		iv_More = (ImageView) findViewById(R.id.iv_More);
		iv_Pay = (ImageView) findViewById(R.id.iv_Pay);
		iv_Me = (ImageView) findViewById(R.id.iv_Me);
		tv_More = (TextView) findViewById(R.id.tv_More);
		tv_Pay = (TextView) findViewById(R.id.tv_Pay);
		tv_Me = (TextView) findViewById(R.id.tv_Me);
		linearLayout_More = (LinearLayout) findViewById(R.id.linearLayout_More);
		linearLayout_Pay = (LinearLayout) findViewById(R.id.linearLayout_Pay);
		linearLayout_Me = (LinearLayout) findViewById(R.id.linearLayout_Me);
		
		layout_Add = (LinearLayout) findViewById(R.id.layout_Add);
		iv_TopImageView = (ImageView) findViewById(R.id.iv_TopAdd);
		
		linearLayout_More.setOnClickListener(this);
		linearLayout_Pay.setOnClickListener(this);
		linearLayout_Me.setOnClickListener(this);
		layout_Add.setOnClickListener(this);
	}
	
	private void initEvent() {
		fragmentList = new ArrayList<Fragment>();
		Fragment payFragment = new PayFragment();
		Fragment otherFragment = new OtherFragment();
		Fragment meFragment = new MeFragment();
		fragmentList.add(otherFragment);
		fragmentList.add(payFragment);
		fragmentList.add(meFragment);
		
		mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
		cleanSelectICON();
		selectIndex(1);
		mViewPager.setOnPageChangeListener(myPageChangeListener);
	}
	
	private OnPageChangeListener myPageChangeListener = new OnPageChangeListener() {
		
		@Override
		public void onPageSelected(int arg0) {
			cleanSelectICON();
			selectIndex(arg0);
			
			if(arg0 == 2){
				iv_TopImageView.setImageResource(R.drawable.edit);
			} else {
				iv_TopImageView.setImageResource(R.drawable.add);
			}
		}
		
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {}
		
		@Override
		public void onPageScrollStateChanged(int arg0) {}
	};
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.linearLayout_More:
			cleanSelectICON();
			selectIndex(0);
			break;
			
		case R.id.linearLayout_Pay:
			cleanSelectICON();
			selectIndex(1);
			break;
			
		case R.id.linearLayout_Me:
			cleanSelectICON();
			selectIndex(2);
			break;
			
		case R.id.layout_Add:
			if(mViewPager.getCurrentItem() != 2){
				Intent newIntent = new Intent(this, PayNewActivity.class);
				startActivity(newIntent);
			} else {
				//此处为编辑
			}
			
			break;

		default:
			break;
		}
	}
	
	/** 清楚选中项文字的颜色、图标的背景 */
	private void cleanSelectICON(){
		tv_More.setTextColor(Color.rgb(153, 153, 153));
		tv_Pay.setTextColor(Color.rgb(153, 153, 153));
		tv_Me.setTextColor(Color.rgb(153, 153, 153));
		
		iv_More.setImageResource(R.drawable.more_unpressed);
		iv_Pay.setImageResource(R.drawable.pay_unpressed);
		iv_Me.setImageResource(R.drawable.me_unpressed);
	}
	
	/**设置选中Fragment的背景和文字颜色*/
	private void selectIndex(int index){
		switch (index) {
		case 0:
			tv_More.setTextColor(Color.rgb(69, 192, 26));
			iv_More.setImageResource(R.drawable.more_pressed);
			mViewPager.setCurrentItem(index);
			break;
			
		case 1:
			tv_Pay.setTextColor(Color.rgb(69, 192, 26));
			iv_Pay.setImageResource(R.drawable.pay_pressed);
			mViewPager.setCurrentItem(index);
			break;
			
		case 2:
			tv_Me.setTextColor(Color.rgb(69, 192, 26));
			iv_Me.setImageResource(R.drawable.me_pressed);
			mViewPager.setCurrentItem(index);
			break;

		default:
			break;
		}
	}
	
	public static MainActivity getInstance(){
		return instance;
	}
	
	/** 退出MainActivity */
	public void exit(){
		instance.finish();
//		instance.onDestroy();
	}
}
